package com.example.xymarket.adapter;

import java.util.HashMap;
import java.util.List;

import com.example.xymarket.R;
import com.example.xymarket.po.Goods;
import com.example.xymarket.httpUtil.AsyncImageLoader;
import com.example.xymarket.httpUtil.CartService;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.common.MyUrl;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GoodsAdapter extends BaseAdapter {

    public List<Goods> items;
    Context context;
    public HashMap<Integer, Drawable> hm;
    int count = 0;
    int layout= 0;
    Handler contextHandler;
    public GoodsAdapter(List<Goods> items, Context context,int layout,Handler handler) {
        super();
        this.items = items;
        this.context = context;
        hm = new HashMap<Integer, Drawable>();
        this.layout=layout;
        count = items.size();
        this.contextHandler=handler;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            Goods goods = items.get(position);
            String url = MyUrl.ImagesUrl + goods.getImage();
            MyViewHolder viewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(layout, null);
            viewHolder.id = goods.getId();
            viewHolder.name = (TextView) convertView.findViewById(R.id.order_name);
            viewHolder.name.setText(goods.getTitle());
            viewHolder.sold = (TextView) convertView.findViewById(R.id.merchant_dis);
            viewHolder.sold.setText("月销：" + String.valueOf(goods.getSoldQty()));
            viewHolder.price = (TextView) convertView.findViewById(R.id.order_price);
            viewHolder.price.setText(String.valueOf(goods.getPrice() / 100) + "." + String.valueOf(goods.getPrice() % 100));
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.order_avatar);
            if (hm.get(position) != null) {
                viewHolder.imageView.setImageDrawable(hm.get(position));
            } else {
                AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
                asyncImageLoader.loadImage(url, viewHolder.imageView,hm,position);
            }
            viewHolder.sub = (Button) convertView.findViewById(R.id.to_merchant);
            viewHolder.sub.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    insert_cart(viewHolder);
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message=new Message();
                    message.obj=goods.getId();
                    message.what=100;
                    contextHandler.sendMessage(message);
                }
            });
            convertView.setTag(viewHolder);
            return convertView;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                Toast.makeText(context,"添加成功!",Toast.LENGTH_LONG).show();
            }
        }
    };


    void insert_cart(MyViewHolder viewHolder)
    {
        MyHandler myHandler=new MyHandler(context,handler);
        CartService cartService=new CartService(myHandler);
        cartService.InsertCart(null,viewHolder.id,1);
    }
    public final class MyViewHolder {

        int id;
        TextView name;
        TextView sold;
        TextView price;
        ImageView imageView;
        Button sub;
    }
}

